#ifndef WORDPREDICTOR_H
#define WORDPREDICTOR_H

#include <QObject>
#include <vector>
#include <opencv2/opencv.hpp>

//#include "laserkeyboard.h"

#define MAX_WORD_LEN 14
//#define FULL_BAYES

class DataKeyboard;

struct WordNode{
    char t[50];
    float frequency;

    WordNode():frequency(0) {
        memset(t, 0, sizeof(t));
    }

    WordNode(char* text, size_t len, float fre):frequency(fre) {
        memcpy(t, text, len);
        t[len] = '\0';
    }
};

struct TapPos{
    float x, y;
    TapPos(float x, float y): x(x), y(y) {}
    TapPos() {}
};

typedef std::vector<cv::Point2f> Contf;
typedef std::vector<Contf> Contsf;

class WordPredictor : public QObject
{
    Q_OBJECT
public:

    explicit WordPredictor(QObject *parent = nullptr);

    std::vector<std::string> getHintWords() const {
        return this->candidateWords;
    }

    std::string lastInputWord() const {
        return this->lastWord;
    }

    int getCurrentIndex() const {
        return this->curIndex;
    }

    DataKeyboard* dataKeyboard() const {
        return this->m_datakeyboard;
    }



protected:
    std::vector<WordNode> srcWords, tempWords;
    std::vector<WordNode> rawWords[20], useWords[20];
    std::vector<TapPos> inputSequence;
    std::vector<std::string> candidateWords; //hint words

    DataKeyboard* m_datakeyboard;

    TapPos charPosTable[26];
    std::string lastWord;

    int currentLen; //current len of word
    int curIndex; // the index of the highlight word

    void loadWordTable();
    float getPWFromInput(const char* word, size_t len);
    void updateCandidateWords();
    void moveCursorLeft();
    void moveCursorRight();
    void initConts();
    bool isDeleteKey(float x, float y);
    bool isLeftKey(float x, float y);
    bool isRightKey(float x, float y);
    bool isSpaceKey(float x, float y);
    bool isAlphaKey(float x, float y);

    Contsf deleteConts, leftConts, rightConts, spaceConts, alphaConts;



signals:
    void hintWordChanged(int index);
    void candidateWordsChanged();
    void oneWordInputed();

protected slots:
    void updateHintWord(int index);
    void receiveGesture(int index);

public slots:
    void receiveOneTap(float x, float y);
    void clearInput();
    void spaceButtonClicked();
    void deleteOneChar();


};

#endif // WORDPREDICTOR_H
